from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.datasets import load_iris

# 加载数据集
data = load_iris()
X = data['data']  # 输入特征 (花萼长度, 花萼宽度, 花瓣长度, 花瓣宽度)
y = data['target']  # 目标标签 (鸢尾花的种类: 0, 1, 2)

# 数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 初始化随机森林分类器 (一种常用的监督学习模型)
model = RandomForestClassifier(n_estimators=100, random_state=42)
# 使用训练数据训练模型
model.fit(X_train, y_train)

# 使用测试数据进行预测
predictions = model.predict(X_test)
# 评估模型准确率
accuracy = accuracy_score(y_test, predictions)
print(f"模型准确率: {accuracy:.2f}")